package manage;

import logIn.MysqlConnectionTool;
import sun.plugin.dom.html.HTMLBodyElement;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 单个员工查询功能实现
 * @Author Jian Chen long
 * @Date 2021/6/18 14:00
 */
public class selectWorker extends JFrame {
    private JLabel logNumlabel = new JLabel("工号");
    private JTextField logNumText = new JTextField(18);
    private JPanel panel = new JPanel();
    private JButton select = new JButton("查询");
    public selectWorker(){
        panel.add(logNumlabel);
        panel.add(logNumText);
        panel.add(select);
        this.add(panel);
        this.setTitle("员工离职");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(450,100);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        /**
         * 查询按钮功能实现
         */
        select.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String logNum = logNumText.getText();
                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                try{
                    if (logNum.length() == 0){
                        JOptionPane.showMessageDialog(null,"填写完整信息");
                    }else{
                        /**
                         * 调用workerInfo方法
                         */
                        new workerInfo(logNum).setVisible(true);
                        logNumText.setText(null);
                    }
                }catch (Exception E){
                    JOptionPane.showMessageDialog(null,"信息不完整,请重试");
                    return;
                } finally {
                    MysqlConnectionTool.close(conn,ps,rs);
                }
            }
        });
    }
}

/**
 * 弹出单个员工信息框
 */
class workerInfo extends selectWorker{
    private JTextArea select = new JTextArea();
    public workerInfo(String logNum){
        this.add(select);
        this.setTitle("员工信息");
        select.setText("编号\t" + "姓名\t" + "联系方式\n");
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(300,280);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = MysqlConnectionTool.getConnection();
            int log = Integer.parseInt(logNum);
            // 获取数据库操作对象
            String sql = "select * from userview where 编号 = " + log;
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                select.append(rs.getString("编号") + "\t"
                        + rs.getString("姓名") + "\t"
                        + rs.getString("联系方式") + "\n");
            }
        }catch (Exception E){
            JOptionPane.showMessageDialog(null,"员工工号错误,请重新输入");
            return;
        }finally {
            MysqlConnectionTool.close(conn,ps,rs);
        }
    }
}
